package com.eastfair.messagecenter.service.impl;

import com.eastfair.boot.service.SuperCacheServiceImpl;
import com.eastfair.cache.messagecenter.MsgSendStatusCacheKeyBuilder;
import com.eastfair.cache.messagecenter.MsgSmsTemplateCacheKeyBuilder;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.model.CacheKeyBuilder;
import com.eastfair.database.mybatis.conditions.Wraps;
import com.eastfair.database.mybatis.conditions.query.QueryWrap;
import com.eastfair.messagecenter.dao.SmsTemplateMapper;
import com.eastfair.messagecenter.entity.MsgService;
import com.eastfair.messagecenter.entity.SmsTemplate;
import com.eastfair.messagecenter.service.SmsTemplateService;
import com.eastfair.boot.service.SuperServiceImpl;

import com.eastfair.uid.baidu.rule.SnowFlakeRole;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;

import java.util.Collection;

//import com.baomidou.dynamic.datasource.annotation.DS;
import org.springframework.aop.framework.AopContext;
import com.eastfair.annotation.annotation.cache.Magic;
import com.eastfair.annotation.annotation.cache.Cache;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
//import com.baomidou.dynamic.datasource.annotation.DS;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

import com.eastfair.messagecenter.vo.*;
import com.eastfair.messagecenter.dto.*;

/**
 * <p>
 * 业务实现类
 * 短信模板
 * </p>
 *
 * @author ligang
 * @date 2022-09-08
 */
@Slf4j
@Service
//@DS("#thread.tenant")
public class SmsTemplateServiceImpl extends SuperCacheServiceImpl<SmsTemplateMapper, SmsTemplate> implements SmsTemplateService {
    @Autowired
    SnowFlakeRole snowFlakeRole;
    /**
     * 处理新增相关处理
     *
     * @param model 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerSave(SmsTemplate model) {
        model.setId(snowFlakeRole.findUidSimple());
        model.setProjectId(ContextUtil.getProjectId());
        model.setSubsystemId(ContextUtil.getSubSystemId());
        model.setTeamId(ContextUtil.getTeamId());
        model.setIsDeleted(BusinessConstant.NO);
        model.setIsEnabled(BusinessConstant.YES);
        //雪花ID
        return R.successDef();
    }

    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<SmsTemplate> modelList) {
        modelList.stream().forEach(s -> {
            s.setIsDeleted(BusinessConstant.YES);
        });
        // TODO: 2021/10/29 这里需要操作其他业务，如清空角色等
        return R.successDef();
    }

    /**
     * 通用查询
     *
     * @param modelDTO DTO
     * @param ids      主键id集合
     * @return list
     */
    @Override
    public List<SmsTemplate> list(SmsTemplateDTO modelDTO, List<Long> ids) {
        return ((SmsTemplateService) AopContext.currentProxy()).listOfCache(modelDTO, ids);
    }


    /**
     * 缓存查询
     *
     * @param modelDTO DTO
     * @param ids      主键id集合
     * @return list
     */
    @Override
    @Cache(key = "",
            magic = @Magic(
                    key = "{" +
                            "{'bizType', #args[0]?.bizType}," +
                            "{'name', #args[0]?.name}," +
                            "{'content', #args[0]?.content}," +
                            "{'systemScene', #args[0]?.systemScene}," +
                            "{'templateParams', #args[0]?.templateParams}," +
                            "{'templateCode', #args[0]?.templateCode}," +
                            "{'signName', #args[0]?.signName}," +
                            "{'templateDescribe', #args[0]?.templateDescribe}," +
                            "{'teamId', #args[0]?.teamId}," +
                            "{'isEnabled', #args[0]?.isEnabled}," +
                            "{'systemScene', #args[0]?.systemScene}," +
                            "{'id', #args[1]}" +
                            "}"))
    public List<SmsTemplate> listOfCache(SmsTemplateDTO modelDTO, List<Long> ids) {
        //查询
        SmsTemplate smsTemplate = new SmsTemplate();
        if (modelDTO != null) {
            BeanUtils.copyProperties(modelDTO, smsTemplate);
        }
        QueryWrap<SmsTemplate> queryWrapper = Wraps.q(smsTemplate);
        if (ids != null && !ids.isEmpty()) {
            queryWrapper.lambda().in(SmsTemplate::getId, ids);
        }
        queryWrapper.lambda().eq(SmsTemplate::getIsDeleted, BusinessConstant.DELETE_NO);
        List<SmsTemplate> modelList = list(queryWrapper);
        return modelList;
    }


    /**
     * do转vo
     *
     * @param model model
     * @return modelVo
     */
    @Override
    public SmsTemplateVO doToVo(SmsTemplate model) {
        if (model == null) {
            return null;
        }
        SmsTemplateVO modelVo = new SmsTemplateVO();
        BeanUtils.copyProperties(model, modelVo);
        modelVo.setId(model.getId());
        return modelVo;
    }

    /**
     * do转vo
     *
     * @param modelList modelList
     * @return list
     */
    @Override
    public List<SmsTemplateVO> doToVos(List<SmsTemplate> modelList) {
        if (modelList == null || modelList.isEmpty()) {
            return null;
        }
        List<SmsTemplateVO> result = new ArrayList<>();
        for (SmsTemplate model : modelList) {
            result.add(doToVo(model));
        }
        return result;
    }

    @Override
    public CacheKeyBuilder getCacheKeyBuilder() {
        return new MsgSmsTemplateCacheKeyBuilder();
    }
}
